package com.yonyou.component;

import com.intellij.notification.NotificationType;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.NlsSafe;
import com.intellij.openapi.wm.StatusBar;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.vcs.log.Hash;
import git4idea.GitCommit;
import git4idea.GitLocalBranch;
import git4idea.GitUtil;
import git4idea.branch.GitBranchUtil;
import git4idea.commands.*;
import git4idea.push.GitPushParams;
import git4idea.push.GitPushParamsImpl;
import git4idea.push.GitPushSource;
import git4idea.repo.GitRemote;
import git4idea.repo.GitRepository;
import org.apache.commons.collections.CollectionUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class CheckOutProcess extends Task.Backgroundable {



    private Project project;
    private String[] selectedValues;
    private List<Hash> commitIDS;
    private  Git git ;
    private GitRepository repository;
    private java.util.LinkedHashMap linkedHashMap ;
    public CheckOutProcess(java.util.LinkedHashMap linkedHashMap ,GitRepository repository, Git git, @Nullable Project project, @Nls(capitalization = Nls.Capitalization.Title) @NotNull String title , List<Hash> commitIDS, String[] selectedValues) {
        super(project, title);
        this.project = project;
        this.git = git;
        this.selectedValues = selectedValues;
        this.commitIDS = commitIDS;
        this.repository = repository;
        this.linkedHashMap = linkedHashMap;
    }

    @Override
    public void run(@NotNull ProgressIndicator progressIndicator) {
        StatusBar statusBar = WindowManager.getInstance().getStatusBar(project);
        // 设置进度条为模糊进度条
        progressIndicator.setIndeterminate(true);
        try {
            GitLocalBranch localB = repository.getCurrentBranch();
            String branchN = localB.getName();
            linkedHashMap.remove("master");
            if(selectedValues[0].contains("全选"))
                linkedHashMap.remove("全选");
            else{
                java.util.LinkedHashMap newlinkedHashMap=new java.util.LinkedHashMap();
                for(String a :selectedValues){
                    newlinkedHashMap.put(a,a);
                }
                linkedHashMap=newlinkedHashMap;
            }
            ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
            indicator.setIndeterminate(false);
            GitLineHandlerListener progressListener = GitStandardProgressAnalyzer.createListener(indicator);

            for(Object branch:linkedHashMap.keySet()){
               String  branchName=(String)branch;
               GitCommandResult result =git.checkout(repository, branchName,null,true, false,progressListener);
                if (result.success()) {
                    Thread.sleep(1000);
                    List<String> commitsStr = commitIDS.stream().map(Hash::asString).collect(Collectors.toList());
                    for(String str :commitsStr){
                        GitCommandResult result1 = git.cherryPick(repository, str, true, false,progressListener);
                        if (result1.success()) {
                            com.yonyou.ctm.ctmplugin.utils.NotificationUtil.showNotification(NotificationType.INFORMATION,branchName+"::Cherry-pick successful!");

                            GitCommandResult gitresult = git.push(repository, "origin", "", branchName, true);
                           // git.push(repository,remoteName,branchName,":" + branchName,true,null,progressListener);
                        } else {
                            com.yonyou.ctm.ctmplugin.utils.NotificationUtil.showNotification(NotificationType.ERROR,"Cherry-pick failed: " + result.getErrorOutputAsJoinedString());
                        }
                    }
                }

            }
            // 开启进度条
            progressIndicator.setIndeterminate(true);
            // 设置进度条为百分百
            progressIndicator.setFraction(1.0);
            // progressIndicator.setText(line);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
